import router from "."
import store from "../store"
import {
  Message
} from "element-ui"
import {
  getToken,
  removeToken
} from "@/utils/auth"

const whiteList = ["/login", "/sso/logout"]
// let reg = /(login\?code=|login\/\?code=)/
let num = 0
let searchTop = window.location.search.split("?")
if (searchTop.length === 2) {
  // 统一登录用户 去除token从新登录
  let arr = searchTop[1].split("&")
  if (arr.some(a => a.split("=")[0] === "code")) {
    if (num === 0) {
      removeToken()
    }
  }
}

router.beforeEach((to, from, next) => {
  // console.log(reg.test('login/?code='), 'to')
  // console.log(window.location, 'window')
  // // 路由跳转统一处理
  // if (reg.test(window.location.href)) {
  //   // 判断是否为 ?code=  登录
  //   if (to.name !== 'home') {
  //     removeToken()
  //   }
  //   next()
  // }
  if (getToken()) {
    // console.log('有token')
    if (to.path === "/login") {
      next({
        path: "/"
      })
    } else if (to.path === "/login/") {
      next({
        path: "/"
      })
    } else if (to.path === "/sso/logout") {
      next()
    } else if (store.getters.navBar.length === 0) {
      // JSON.parse(window.sessionStorage.getItem('sideNav')).length === 0
      store.dispatch("GetUserInfo").then(res => { // dispatch 触发获取当前用户信息
        store.dispatch("GetNav", getToken()).then((res) => { // dispatch 触发获取目录栏
          if (window.location.search) {
            window.location.href = window.location.href.replace( // path跳转
              window.location.search,
              ""
            )
          }
          next()
        }).catch((err) => {
          removeToken()
          Message.error(err)
          next({
            path: "/login"
          })
        })
      }).catch(() => {
        store.dispatch("LogOut").then(() => {
          Message.error("验证失败, 请重新登录")
          next({
            path: "/login"
          })
        })
      })
    } else if (to.path.substring(0, 15) === "/zxManage/zxlm_") {
      next({
        path: "/zxManage"
      })
    } else {
      next()
    }
  } else {
    let search = window.location.search.split("?")
    if (whiteList.indexOf(to.path) !== -1) {
      next()
    } else if (search.length === 2) {
      let obj = {}
      let arr = search[1].split("&")
      // 统一登录-code登录
      if (arr.some(a => a.split("=")[0] === "code")) {
        // console.log('走code')
        obj.code = arr.filter(a => a.split("=")[0] === "code")[0].split("=")[1]
        // store.dispatch('loginByCode', obj)
        store.dispatch("loginSecurity", { username: obj.code })
          .then(res => {
            // console.log(res, '登陆返回的时 的token')
            store.dispatch("GetUserInfo").then(res => { // dispatch 触发获取当前用户信息
              store.dispatch("GetNav", res).then((res) => { // dispatch 触发获取目录栏
                num++
                next({
                  path: "/"
                })
              }).catch(() => {
                store.dispatch("LogOut").then(() => {
                  Message.error("验证失败, 请重新登录")
                  next({
                    path: "/login"
                  })
                })
              })
            })
          }).catch(() => {
            Message.error("登录失败")
            next({
              path: "/login"
            })
          })
      } else if (arr.some(a => a.split("=")[0] === "name")) {
        // 统一登录-用户名密码登录
        // console.log('走name')
        obj.username = arr.filter(a => a.split("=")[0] === "name")[0].split("=")[1]
        obj.password = arr.filter(a => a.split("=")[0] === "passwd")[0].split("=")[1]
        // store.dispatch('loginByUsername', obj).then(res => {
        store.dispatch("loginSecurity", { username: obj.username }).then(res => {
          next({
            path: "/"
          })
        }).catch(() => {
          Message.error("登录失败")
          next({
            path: "/login"
          })
        })
      }
    } else {
      next({
        path: "/login"
      })
    }
  }
})
